<?php
/**
 * Created by PhpStorm.
 * User: BlueIdeaLirui
 * Date: 2019/09/04
 * Time: 14:58
 */

namespace business\elasticsearch\caches;

use common\utils\RedisUtil;

class LandCache
{
    /**
     * 添加STRING记录
     * @param string $stringPrefix
     * @param int $id
     * @param string $data
     * @param int $expire
     * @return bool
     */
    public static function saveRecordByString(
        $stringPrefix = '',
        $id = 0,
        $data = '',
        $expire = 600
    ) {
        $redis = \Yii::$app->redis;

        if (!is_numeric($id) || empty($data)) return false;

        $stringName = $stringPrefix . '_' . $id;

        $redis->set($stringName, $data);
        $redis->expire($stringName, $expire);

        return true;
    }

    /**
     * 添加HAST记录
     * @param string $hashPrefix
     * @param int $id
     * @param array $data
     * @param int $expire
     * @return bool
     */
    public static function saveRecordByHash(
        $hashPrefix = '',
        $id = 0,
        $data = [],
        $expire = 600
    ) {
        $redis = \Yii::$app->redis;

        if (!is_numeric($id) || !is_array($data)) return false;

        $hashName = $hashPrefix . ':' . $id;
        $params = RedisUtil::map2RedisParameter($data);
        array_unshift($params, $hashName);

        $redis->executeCommand('hmset', $params);
        $redis->expire($hashName, $expire);

        static::saveRecordBySet($hashPrefix, $id);

        return true;
    }

    /**
     * 添加SET记录
     * @param string $setPrefix
     * @param int $id
     * @param int $expire
     * @return bool
     */
    public static function saveRecordBySet(
        $setPrefix = '',
        $id = 0,
        $expire = 600
    ) {
        $redis = \Yii::$app->redis;

        if (!is_numeric($id)) return false;

        $setName = $setPrefix . '_sort';

        $redis->zadd($setName, $id, $id);
        $redis->expire($setName, $expire);

        return true;
    }

    /**
     * 获取分页数据
     * @param string $hashPrefix
     * @param int $page
     * @param int $pageSize
     * @param array $key
     * @return array|bool
     */
    public static function getListByHash(
        $hashPrefix = '',
        $page = 1,
        $pageSize = 10,
        $key = []
    ) {
        $redis = \Yii::$app->redis;
        if(!is_numeric($page) || !is_numeric($pageSize)) return false;

        $from   = ($page - 1) * $pageSize;
        $offset = ($from + $pageSize) - 1;

        // 指定区间内，带有 score 值(可选)的有序集成员的列表
        $range = $redis->zrange($hashPrefix . '_sort', $from, $offset);
        // 统计ScoreSet总数
        $count = $redis->zcard($hashPrefix . '_sort');
        // 总共多少页
        $pageCount = ceil($count / $pageSize);

        // 分页数据
        $pageList = [];

        if (!empty($range)) {
            foreach ($range as $qid) {
                if (count($key) > 0) {
                    $pageList[] = $redis->hmget($hashPrefix . ':' . $qid, $key);
                } else {
                    $pageList[] = $redis->hgetall($hashPrefix . ':' . $qid);
                }
            }
        }

        $data = [
            'totalCount' => 0,
            'totalArea'  => 0,
            'totalFee'   => 0,
            'data'       => $pageList,
            'page'       => [
                'page'      => $page, //当前页数
                'pageSize'  => $pageSize, //每页多少条
                'count'     => $count, //记录总数
                'pageCount' => $pageCount //总页数
            ]
        ];

        return $data;
    }

    public static function getRecordByHash($hashPrefix = '', $id = 0)
    {
        $redis = \Yii::$app->redis;
        return $redis->hgetall($hashPrefix . '_' . $id);
    }

    public static function getRecordBySet($setPrefix = '', $id = 0)
    {
        $redis = \Yii::$app->redis;
    }

    public static function getRecordByString($stringPrefix = '', $id = 0)
    {
        $redis = \Yii::$app->redis;
        return $redis->get($stringPrefix . '_' . $id);
    }

    public static function delRecordByHash()
    {
        $redis = \Yii::$app->redis;
    }

    /**
     * 清空数据
     * @param string $type
     * @return bool
     */
    public static function clear($type = 'db')
    {
        $redis = \Yii::$app->redis;
        if ($type == 'db') {
            $redis->flushdb();
        } elseif ($type == 'all') {
            $redis->flushall();
        } else {
            return false;
        }
        return true;
    }
}
